package com.wxdc.nmgyt.nmgytwydc.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wxdc.nmgyt.nmgytwydc.IMapFunInterface.IMapFunInterface;
import com.wxdc.nmgyt.nmgytwydc.R;
import com.wxdc.nmgyt.nmgytwydc.app.MyApp;
import com.wxdc.nmgyt.nmgytwydc.presenter.NavigationHomePersenter;
import com.wxdc.nmgyt.nmgytwydc.presenter.contract.NavigationHomeContract;
import com.wxdc.nmgyt.nmgytwydc.utils.GlideCacheUtil;
import com.wxdc.nmgyt.nmgytwydc.utils.Network;
import com.wxdc.nmgyt.nmgytwydc.utils.SharedPreferenceUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class NavigationHomeFragment extends BaseFragment<NavigationHomePersenter> implements NavigationHomeContract.View, OnBannerListener, SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    @BindView(R.id.banner)
    Banner mBanner;
    @BindView(R.id.navigation_home_noticeinfo_txt)
    TextView mNavigationHomeNoticeinfoTxt;
    @BindView(R.id.navigation_home_refreshlayout)
    SwipeRefreshLayout mNavigationHomeRefreshlayout;
    private ArrayList<String> list_path;
    private ArrayList<String> list_title;
    private AppCompatActivity mActivity;

    @Override
    public int getmLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (AppCompatActivity) getActivity();
        GlideCacheUtil.getInstance().clearImageAllCache(MyApp.getInstance().getApplicationContext());
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mToolbarTitle.setText(R.string.main_toolbar_title);
        initView();
        //设置SwipeRefreshLayout
        mNavigationHomeRefreshlayout.setOnRefreshListener(this);
        mNavigationHomeRefreshlayout.setColorSchemeColors(getResources().getColor(R.color.paleturquoise));
    }

    private void initView() {
        //放图片地址的集合
        getImageList();
        //放标题的集合
        list_title = new ArrayList<>();
        list_title.add(getString(R.string.main_home_title1));
        list_title.add(getString(R.string.main_home_title2));
        list_title.add(getString(R.string.main_home_title3));
        list_title.add(getString(R.string.main_home_title4));
        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
        //设置图片网址或地址的集合
        mBanner.setImages(list_path);
        //设置图片加载器，图片加载器在下方
        mBanner.setImageLoader(new MyLoader());
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        mBanner.setBannerAnimation(Transformer.Default);//.Default
        //设置轮播图的标题集合
        mBanner.setBannerTitles(list_title);
        //设置轮播间隔时间
        mBanner.setDelayTime(2000);
        //设置是否为自动轮播，默认是“是”。
        mBanner.isAutoPlay(true);
        //设置指示器的位置，小点点，左中右。
        mBanner.setIndicatorGravity(BannerConfig.CENTER)
                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                .setOnBannerListener(this)
                //必须最后调用的方法，启动轮播图。
                .start();
        mPersenter.getNoticeContent();//请求服务器，获取最新公告信息
    }

    //轮播图的监听方法
    @Override
    public void OnBannerClick(int position) {
        Log.i("tag", "你点了第" + position + "张轮播图");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick({R.id.home_data_click, R.id.home_outline_click, R.id.home_localline_click, R.id.home_collection_click})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.home_data_click:
                if (mActivity instanceof IMapFunInterface) {
                    ((IMapFunInterface) mActivity).switchMenu("dataCollection");
                }
                break;
            case R.id.home_outline_click:
                if (mActivity instanceof IMapFunInterface) {
                    ((IMapFunInterface) mActivity).switchMenu("baiduMap");
                }
                break;
            case R.id.home_localline_click:
                if (mActivity instanceof IMapFunInterface) {
                    ((IMapFunInterface) mActivity).switchMenu("localArcgisMap");
                }
                break;
            case R.id.home_collection_click:
                if (mActivity instanceof  IMapFunInterface) {
                    ((IMapFunInterface)mActivity).switchMenu("management");
                }
                break;
        }
    }

    /**
     * 获得存放图片地址的集合
     */
    private void getImageList() {
        if (list_path == null) {
            list_path = new ArrayList<>();
        }
        list_path.clear();
        list_path.add(MyApp.getInstance().getHttpAddress() + "/photo/banner_image1.jpg");
        list_path.add(MyApp.getInstance().getHttpAddress() + "/photo/banner_image2.jpg");
        list_path.add(MyApp.getInstance().getHttpAddress() + "/photo/banner_image3.jpg");
        list_path.add(MyApp.getInstance().getHttpAddress() + "/photo/banner_image4.jpg");
    }

    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        mBanner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        //停止轮播
        mBanner.stopAutoPlay();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * @param msg 请求服务器失败返回错误信息
     */
    @Override
    public void showError(String msg) {
        mNavigationHomeNoticeinfoTxt.setText("\u3000\u3000" + msg);
        if (mNavigationHomeRefreshlayout != null) {
            mNavigationHomeRefreshlayout.setRefreshing(false);
        }
    }

    /**
     * @param noticeInfo 请求服务器返回公告信息
     */
    @Override
    public void showNoticeContent(String noticeInfo) {
        // 一个\u3000代表一个空字符
        mNavigationHomeNoticeinfoTxt.setText("\u3000\u3000" + noticeInfo);
        mNavigationHomeRefreshlayout.setRefreshing(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        if (!Network.isConnected(mContext)) {
            MyApp.getInstance().showToastLongMessage(mContext, getResources().getString(R.string.no_network));
            return;
        }
        mPersenter.getNoticeContent();
    }

    //自定义的图片加载器
    private class MyLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }
}
